package org.itboys.basetemplate.web;

import org.itboys.admin.entity.BusinessCenterUser;
import org.itboys.admin.service.BusinessCenterUserService;
import org.itboys.admin.tools.AdminSessionHolder;
import org.itboys.basetemplate.entity.Voucher;
import org.itboys.basetemplate.service.VoucherService;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mongodb.utils.page.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 代金券管理
 * 
 * @author Administrator
 * 
 */

@RestController
@RequestMapping("/VoucherController")
public class VoucherController extends BaseController {

	@Autowired
	private VoucherService voucherService;
	@Autowired
	private BusinessCenterUserService businessCenterUserService;

	@RequestMapping("/toVM")
	public ModelAndView listvm(HttpServletRequest request, HttpServletResponse response, Model model) {
		return new ModelAndView("/adve/voucher");
	}

	/**
	 * 分页查询
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping("/list")
	public void list(HttpServletRequest request, HttpServletResponse response) {
		BusinessCenterUser user = businessCenterUserService.getById(AdminSessionHolder.getAdminUserId());
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("centerId", user.getCenterId());
		Page<Voucher> page = voucherService.pageQuery(sqlMap, request);
		JsonPageUtils.renderJsonPage(page.getTotal(), page.getData(), response);
	}

	/**
	 * 新增修改时保存
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping("/save")
	public void save(@ModelAttribute Voucher voucher, HttpServletRequest request, HttpServletResponse response) {
		try {
			BusinessCenterUser user = businessCenterUserService.getById(AdminSessionHolder.getAdminUserId());
			voucher.setCenterId(user.getCenterId());
			if(voucher.getId() != 0) {
				voucherService.update(voucher);
			} else {
				voucherService.save(voucher);
			}
			AjaxUtils.renderText(response, "success");
		} catch (Exception e) {
			e.printStackTrace();
			AjaxUtils.renderText(response, "fail");
		}
	}

	/**
	 * 根据Id查询
	 * 
	 * @param id
	 * @param request
	 * @param response
	 */
	@RequestMapping("/findById")
	public void findById(@RequestParam Long id, HttpServletRequest request, HttpServletResponse response) {
		Voucher voucher = voucherService.getById(id);
		AjaxUtils.renderJson(response, voucher);
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @param request
	 * @param response
	 */
	@RequestMapping("/delete")
	public void delete(@RequestParam Long id, HttpServletRequest request, HttpServletResponse response) {
		try {
			voucherService.delete(id);
			AjaxUtils.renderText(response, "success");
		} catch (Exception e) {
			AjaxUtils.renderText(response, "fail");
		}
	}
	
	/**
	 * 获取所有折扣券列表
	 * @param request
	 * @param response
	 */
	@RequestMapping("/getAll")
	public void getAll(HttpServletRequest request, HttpServletResponse response) {
		BusinessCenterUser user = businessCenterUserService.getById(AdminSessionHolder.getAdminUserId());
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("centerId", user.getCenterId());
		List<Voucher> list = voucherService.list(sqlMap);
		AjaxUtils.renderJson(response, list);
	}

}
